<template>
  <el-form
    :model="form"
    ref="form"
    :rules="rules"
    class="form"
  >
    <el-form-item
      class="form-item"
      prop="username"
    >
      <el-input
        placeholder="用户名手机"
        v-model="form.username"
        @focus="clearrules('username')"
      >
      </el-input>
    </el-form-item>

    <el-form-item
      class="form-item"
      prop="captcha"
    >
      <el-input
        placeholder="验证码"
        v-model="form.captcha"
        @focus="clearrules('captcha')"
      >
        <template slot="append">
          <el-button @click="handleSendCaptcha">
            发送验证码
          </el-button>
        </template>
      </el-input>
    </el-form-item>

    <el-form-item
      class="form-item"
      prop="nickname"
    >
      <el-input
        placeholder="你的名字"
        v-model="form.nickname"
        @focus="clearrules('nickname')"
      >
      </el-input>
    </el-form-item>

    <el-form-item
      class="form-item"
      prop="password"
    >
      <el-input
        placeholder="密码"
        type="password"
        v-model="form.password"
        @focus="clearrules('password')"
      ></el-input>
    </el-form-item>

    <el-form-item
      class="form-item"
      prop="checkPassword"
    >
      <el-input
        placeholder="确认密码"
        type="password"
        v-model="form.checkPassword"
        @focus="clearrules('checkPassword')"
      >
      </el-input>
    </el-form-item>

    <el-button
      class="submit"
      type="primary"
      @click="handleRegSubmit"
    >
      注册
    </el-button>
  </el-form>
</template>

<script>
import { register, sendCaptcha } from "@/api/user";
export default {
  data() {
    const validatePass = (rule, value, callback) => {
      if (value == "") {
        callback(new Error("请再次输入密码"));
      } else if (value != this.form.password) {
        callback(new Error("两次密码不一致"));
      } else {
        callback();
      }
    };
    return {
      // 表单数据
      form: {
        username: "",
        nickname: "",
        captcha: "",
        password: "",
        checkPassword: "",
      },
      // 表单规则
      rules: {
        username: [
          { required: true, message: "请输入正确的手机号码", trigger: "blur" },
        ],
        nickname: [{ required: true, message: "请输入昵称", trigger: "blur" }],
        captcha: [{ required: true, message: "请输入验证码", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        checkPassword: [{ validator: validatePass, trigger: "blur" }],
      },
    };
  },
  methods: {
    // 发送验证码
    async handleSendCaptcha() {
      const regexp = /^1[35789]\d{9}$/;
      if (!regexp.test(this.form.username)) {
        return this.$message.warning("请输入正确的手机号码");
      }
      let res = await sendCaptcha(this.form.username);
      if (res) {
        this.$message.success("收到手机验证码：" + res.data.code);
      }
    },

    // 注册
    handleRegSubmit() {
      this.$refs.form.validate(async (v) => {
        if (v) {
          let { checkPassword, ...data } = this.form;
          let res = await register(data);
          if (res) {
            this.$message.success("注册成功");
            this.$emit("successReg");
          }
        } else {
          this.$message.warning("请确认表单是否填写完整");
        }
      });
    },

    // 去除验证规则
    clearrules(v) {
      this.$refs.form.clearValidate(v);
    },
  },
};
</script>

<style scoped lang="less">
.form {
  padding: 25px;
}

.form-item {
  margin-bottom: 20px;
}

.form-text {
  font-size: 12px;
  color: #409eff;
  text-align: right;
  line-height: 1;
}

.submit {
  width: 100%;
  margin-top: 10px;
}
</style>